package ch.nostromo.tiffanys.engines.tiffanys.ai;

import ch.nostromo.tiffanys.engines.tiffanys.board.TiffanysBoard;
import ch.nostromo.tiffanys.game.board.Board;

public class Evaluation {

  public static int evaluatePosition(long[] board, int currentColor) {

    int whitePos = 0;
    int blackPos = 0;

    for (int i = 21; i <= 98; i++) {

      if ((Board.OWNER_WHITE & board[i]) != 0) {

        if ((Board.PIECE_PAWN & board[i]) != 0) {
          whitePos += TiffanysAiTools.PAWN_WHITE_VALUES[i];
        } else if ((Board.PIECE_ROOK & board[i]) != 0) {
          whitePos += TiffanysAiTools.ROOK_WHITE_VALUES[i];
        } else if ((Board.PIECE_BISHOP & board[i]) != 0) {
          whitePos += TiffanysAiTools.BISHOP_WHITE_VALUES[i];
        } else if ((Board.PIECE_KNIGHT & board[i]) != 0) {
          whitePos += TiffanysAiTools.KNIGHT_WHITE_VALUES[i];
        } else if ((Board.PIECE_KING & board[i]) != 0) {
          whitePos += TiffanysAiTools.KING_WHITE_VALUES[i];
        }

      } else if ((Board.OWNER_BLACK & board[i]) != 0) {

        if ((Board.PIECE_PAWN & board[i]) != 0) {
          blackPos += TiffanysAiTools.PAWN_BLACK_VALUES[i];
        } else if ((Board.PIECE_ROOK & board[i]) != 0) {
          blackPos += TiffanysAiTools.ROOK_BLACK_VALUES[i];
        } else if ((Board.PIECE_BISHOP & board[i]) != 0) {
          blackPos += TiffanysAiTools.BISHOP_BLACK_VALUES[i];
        } else if ((Board.PIECE_KNIGHT & board[i]) != 0) {
          blackPos += TiffanysAiTools.KNIGHT_BLACK_VALUES[i];
        } else if ((Board.PIECE_KING & board[i]) != 0) {
          blackPos += TiffanysAiTools.KING_BLACK_VALUES[i];
        }

      }

    }

    if (board[TiffanysBoard.IDX_WHITE_CASTELING] == 1) {
      whitePos += 100;
    }

    if (board[TiffanysBoard.IDX_BLACK_CASTELING] == 1) {
      blackPos += 100;
    }

    int result = 0;

    if (currentColor == Board.OWNER_WHITE) {
      result = (int) (board[TiffanysBoard.IDX_WHITE_MATERIAL] - board[TiffanysBoard.IDX_BLACK_MATERIAL]);
      result += whitePos;
      result -= blackPos;
    } else {
      result = (int) (board[TiffanysBoard.IDX_BLACK_MATERIAL] - board[TiffanysBoard.IDX_WHITE_MATERIAL]);
      result += blackPos;
      result -= whitePos;
    }

    return result;
  }

}
